package com.yonyou.brigade.facade.util;

import com.yonyou.brigade.facade.model.ApplyInfo;
import com.yonyou.brigade.facade.model.Passenger;
import com.yonyou.brigade.third_parties.yihao.model.CLYHApplyInfo;
import com.yonyou.brigade.third_parties.yihao.model.CLYHApprovalInfo;
import com.yonyou.ybzboot.starter.common.exception.BusinessException;
import corp.openapicalls.contract.Authentification;
import corp.openapicalls.contract.setapproval.request.*;
import corp.openapicalls.service.setapproval.SetApprovalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class ModelConvert {

    private static Logger logger=LoggerFactory.getLogger(ModelConvert.class);

    public static ApplyInfo convertMapToApplyInfo(Map applyInfoMap,String tenant)
    {
        ApplyInfo applyInfo=null;
        if(applyInfoMap!=null)
        {
            applyInfo=new ApplyInfo();
            applyInfo.setTenant(tenant);
            applyInfo.setApprovalNumber(applyInfoMap.get("approvalNumber").toString());
            applyInfo.setMatters(getMapFiledValue(applyInfoMap.get("matters")));
            applyInfo.setFromChannel(getMapFiledValue(applyInfoMap.get("fromChannel"),"fromChannel",true));
            applyInfo.setPhone(getMapFiledValue(applyInfoMap.get("phone"),"phone",true));

            //机票
            ArrayList<ApplyInfo.FlightEndorsementDetail> flightEndorsementDetails=new ArrayList<>();
            List<Map> flightEndorsementDetailsMap=(List<Map>)applyInfoMap.get("flightEndorsementDetails");
            if(flightEndorsementDetailsMap!=null&&flightEndorsementDetailsMap.size()>0)
            {
                for(int i=0;i<flightEndorsementDetailsMap.size();i++)
                {
                    ApplyInfo.FlightEndorsementDetail flightEndorsementDetail=applyInfo.new FlightEndorsementDetail();
                    flightEndorsementDetail.setFlightWay(getMapFiledValue(flightEndorsementDetailsMap.get(i).get("flightWay")));
                    flightEndorsementDetail.setDepartDateBegin(getMapFiledValue(flightEndorsementDetailsMap.get(i).get("departDateBegin")));
                    flightEndorsementDetail.setDepartDateEnd(getMapFiledValue(flightEndorsementDetailsMap.get(i).get("departDateEnd")));
                    flightEndorsementDetail.setFromCities((ArrayList<String>)flightEndorsementDetailsMap.get(i).get("fromCities"));
                    flightEndorsementDetail.setToCities((ArrayList<String>)flightEndorsementDetailsMap.get(i).get("toCities"));

                    //差旅壹号行程序号
                    if (flightEndorsementDetailsMap.get(i).get("travelNo") != null){
                        flightEndorsementDetail.setTravelNo((Integer) flightEndorsementDetailsMap.get(i).get("travelNo"));
                    }

                    ArrayList<Passenger> passengers=new ArrayList<>();
                    ArrayList<Map> passengersMaps=(ArrayList<Map>)flightEndorsementDetailsMap.get(i).get("passengerList");
                    if(passengersMaps!=null&&passengersMaps.size()>0)
                    {
                        for(int j=0;j<passengersMaps.size();j++)
                        {
                            Passenger passenger=new Passenger();
                            passenger.setCredentialsNumber(getMapFiledValue(passengersMaps.get(j).get("credentialsNumber")));
                            passenger.setCredentialsType(getMapFiledValue(passengersMaps.get(j).get("credentialsType")));
                            passenger.setIsEmployee(getMapFiledValue(passengersMaps.get(j).get("isEmployee")));
                            passenger.setName(getMapFiledValue(passengersMaps.get(j).get("name"),"passenger.name",true));
                            passenger.setPhone(getMapFiledValue(passengersMaps.get(j).get("phone")));

                            //补全差旅壹号人员缺失字段
                            if ( passengersMaps.get(j).get("empID") != null){
                                passenger.setEmpID(getMapFiledValue(passengersMaps.get(j).get("empID")));
                            }
                            if ( passengersMaps.get(j).get("department") != null){
                                passenger.setDepartment(getMapFiledValue(passengersMaps.get(j).get("department")));
                            }
                            if ( passengersMaps.get(j).get("departmentNO") != null){
                                passenger.setDepartmentNO(getMapFiledValue(passengersMaps.get(j).get("departmentNO")));
                            }

                            passengers.add(passenger);
                        }
                    }
                    flightEndorsementDetail.setPassengerList(passengers);
                    flightEndorsementDetails.add(flightEndorsementDetail);

                }
                applyInfo.setFlightEndorsementDetails(flightEndorsementDetails);
            }

            //酒店
            ArrayList<ApplyInfo.HotelEndorsementDetail> hotelEndorsementDetails=new ArrayList<>();
            List<Map> hotelEndorsementDetailsMap=(List<Map>)applyInfoMap.get("hotelEndorsementDetails");
            if(hotelEndorsementDetailsMap!=null&&hotelEndorsementDetailsMap.size()>0)
            {
                for(int i=0;i<hotelEndorsementDetailsMap.size();i++)
                {
                    ApplyInfo.HotelEndorsementDetail hotelEndorsementDetail=applyInfo.new HotelEndorsementDetail();
                    hotelEndorsementDetail.setCheckInDateBegin(getMapFiledValue(hotelEndorsementDetailsMap.get(i).get("checkInDateBegin")));
                    hotelEndorsementDetail.setCheckInDateEnd(getMapFiledValue(hotelEndorsementDetailsMap.get(i).get("checkInDateEnd")));
                    hotelEndorsementDetail.setCheckOutDateBegin(getMapFiledValue(hotelEndorsementDetailsMap.get(i).get("checkOutDateBegin")));
                    hotelEndorsementDetail.setCheckOutDateEnd(getMapFiledValue(hotelEndorsementDetailsMap.get(i).get("checkOutDateEnd")));
                    hotelEndorsementDetail.setToCities((ArrayList<String>)hotelEndorsementDetailsMap.get(i).get("toCities"));
                    ArrayList<Passenger> passengers=new ArrayList<>();
                    ArrayList<Map> passengersMaps=(ArrayList<Map>)flightEndorsementDetailsMap.get(i).get("passengerList");
                    if(passengersMaps!=null&&passengersMaps.size()>0)
                    {
                        for(int j=0;j<passengersMaps.size();j++)
                        {
                            Passenger passenger=new Passenger();
                            passenger.setCredentialsNumber(getMapFiledValue(passengersMaps.get(j).get("credentialsNumber")));
                            passenger.setCredentialsType(getMapFiledValue(passengersMaps.get(j).get("credentialsType")));
                            passenger.setIsEmployee(getMapFiledValue(passengersMaps.get(j).get("isEmployee")));
                            passenger.setName(getMapFiledValue(passengersMaps.get(j).get("name"),"passenger.name",true));
                            passenger.setPhone(getMapFiledValue(passengersMaps.get(j).get("phone")));
                            passengers.add(passenger);
                        }
                    }
                    hotelEndorsementDetail.setPassengerList(passengers);
                    hotelEndorsementDetails.add(hotelEndorsementDetail);

                }
                applyInfo.setHotelEndorsementDetails(hotelEndorsementDetails);
            }

            //火车票
            ArrayList<ApplyInfo.TrainEndorsementDetail> trainEndorsementDetails=new ArrayList<>();
            List<Map> trainEndorsementDetailsMap=(List<Map>)applyInfoMap.get("trainEndorsementDetails");
            if(trainEndorsementDetailsMap!=null&&trainEndorsementDetailsMap.size()>0)
            {
                for(int i=0;i<trainEndorsementDetailsMap.size();i++)
                {
                    ApplyInfo.TrainEndorsementDetail trainEndorsementDetail=applyInfo.new TrainEndorsementDetail();
                    trainEndorsementDetail.setDepartDateBegin(getMapFiledValue(trainEndorsementDetailsMap.get(i).get("departDateBegin")));
                    trainEndorsementDetail.setDepartDateEnd(getMapFiledValue(trainEndorsementDetailsMap.get(i).get("departDateEnd")));
                    trainEndorsementDetail.setFromCities((ArrayList<String>)trainEndorsementDetailsMap.get(i).get("fromCities"));
                    trainEndorsementDetail.setToCities((ArrayList<String>)trainEndorsementDetailsMap.get(i).get("toCities"));
                    ArrayList<Passenger> passengers=new ArrayList<>();
                    ArrayList<Map> passengersMaps=(ArrayList<Map>)flightEndorsementDetailsMap.get(i).get("passengerList");
                    if(passengersMaps!=null&&passengersMaps.size()>0)
                    {
                        for(int j=0;j<passengersMaps.size();j++)
                        {
                            Passenger passenger=new Passenger();
                            passenger.setCredentialsNumber(getMapFiledValue(passengersMaps.get(j).get("credentialsNumber")));
                            passenger.setCredentialsType(getMapFiledValue(passengersMaps.get(j).get("credentialsType")));
                            passenger.setIsEmployee(getMapFiledValue(passengersMaps.get(j).get("isEmployee")));
                            passenger.setName(getMapFiledValue(passengersMaps.get(j).get("name"),"passenger.name",true));
                            passenger.setPhone(getMapFiledValue(passengersMaps.get(j).get("phone")));
                            passengers.add(passenger);
                        }
                    }
                    trainEndorsementDetail.setPassengerList(passengers);
                    trainEndorsementDetails.add(trainEndorsementDetail);

                }
                applyInfo.setTrainEndorsementDetails(trainEndorsementDetails);
            }

            //其他
            ArrayList<ApplyInfo.OtherEndorsementDetail> otherEndorsementDetails=new ArrayList<>();
            List<Map> otherEndorsementDetailsMap = (List<Map>)applyInfoMap.get("otherEndorsementDetails");
            if(otherEndorsementDetailsMap!=null&&otherEndorsementDetailsMap.size()>0)
            {
                for(int i=0;i<otherEndorsementDetailsMap.size();i++)
                {
                    ApplyInfo.OtherEndorsementDetail otherEndorsementDetail=applyInfo.new OtherEndorsementDetail();
                    otherEndorsementDetail.setVoyageType(getMapFiledValue(otherEndorsementDetailsMap.get(i).get("voyageType").toString()));
                    otherEndorsementDetail.setTravelTypeName(getMapFiledValue(otherEndorsementDetailsMap.get(i).get("travelTypeName")));
                    otherEndorsementDetail.setDepartDateBegin(getMapFiledValue(otherEndorsementDetailsMap.get(i).get("departDateBegin")));
                    otherEndorsementDetail.setDepartDateEnd(getMapFiledValue(otherEndorsementDetailsMap.get(i).get("departDateEnd")));
                    otherEndorsementDetail.setFromCities((ArrayList<String>)otherEndorsementDetailsMap.get(i).get("fromCities"));
                    otherEndorsementDetail.setToCities((ArrayList<String>)otherEndorsementDetailsMap.get(i).get("toCities"));

                    //差旅壹号行程序号
                    if (otherEndorsementDetailsMap.get(i).get("travelNo") != null){
                        otherEndorsementDetail.setTravelNo((Integer) otherEndorsementDetailsMap.get(i).get("travelNo"));
                    }

                    ArrayList<Passenger> passengers=new ArrayList<>();
                    ArrayList<Map> passengersMaps=(ArrayList<Map>)otherEndorsementDetailsMap.get(i).get("passengerList");
                    if(passengersMaps!=null&&passengersMaps.size()>0)
                    {
                        for(int j=0;j<passengersMaps.size();j++)
                        {
                            Passenger passenger=new Passenger();
                            passenger.setCredentialsNumber(getMapFiledValue(passengersMaps.get(j).get("credentialsNumber")));
                            passenger.setCredentialsType(getMapFiledValue(passengersMaps.get(j).get("credentialsType")));
                            passenger.setIsEmployee(getMapFiledValue(passengersMaps.get(j).get("isEmployee")));
                            passenger.setName(getMapFiledValue(passengersMaps.get(j).get("name"),"passenger.name",true));
                            passenger.setPhone(getMapFiledValue(passengersMaps.get(j).get("phone")));

                            //补全差旅壹号人员缺失字段
                            if ( passengersMaps.get(j).get("empID") != null){
                                passenger.setEmpID(getMapFiledValue(passengersMaps.get(j).get("empID")));
                            }
                            if ( passengersMaps.get(j).get("department") != null){
                                passenger.setDepartment(getMapFiledValue(passengersMaps.get(j).get("department")));
                            }
                            if ( passengersMaps.get(j).get("departmentNO") != null){
                                passenger.setDepartmentNO(getMapFiledValue(passengersMaps.get(j).get("departmentNO")));
                            }

                            passengers.add(passenger);
                        }
                    }
                    otherEndorsementDetail.setPassengerList(passengers);
                    otherEndorsementDetails.add(otherEndorsementDetail);

                }
                applyInfo.setOtherEndorsementDetails(otherEndorsementDetails);
            }
        }


        return  applyInfo;
    }

    private static String getMapFiledValue(Object fieldValue)
    {
        return  getMapFiledValue(fieldValue,"",false);
    }

    private static String getMapFiledValue(Object fieldValue,String fieldName,boolean notEmpty)
    {
        if(fieldValue==null)
        {
            if(notEmpty)
            {
                throw new BusinessException(fieldName+"不能为空");
            }
            return "";
        }
        else
        {
            return  fieldValue.toString();
        }
    }

    public static SetApprovalServiceRequest AppyInfoToCtripBC(ApplyInfo applyInfo,String appKey, String ticket)
    {
        SetApprovalService setapprovalService = new SetApprovalService();
        SetApprovalServiceRequest approvalServiceRequest=new SetApprovalServiceRequest();
        SetApprovalRequest approvalRequest =new SetApprovalRequest();
        approvalRequest.setApprovalNumber(applyInfo.getApprovalNumber());
        approvalRequest.setStatus(1);
        approvalRequest.setEmployeeID(applyInfo.getEmployeeID());
        Authentification authInfo=new Authentification(appKey,ticket);
        approvalRequest.setAuth(authInfo);

        //机票信息
        ArrayList<FlightEndorsementDetail> ctripFlightEndorsementDetails = new ArrayList<>();
        ctripFlightEndorsementDetails=convertFlightEndorsementDetail(applyInfo.getFlightEndorsementDetails());
        //酒店信息
        ArrayList<HotelEndorsementDetail> ctripHotelEndorsementDetails=new ArrayList<>();
        ctripHotelEndorsementDetails=convertHotelEndorsementDetail(applyInfo.getHotelEndorsementDetails());
        //火车票
        ArrayList<TrainEndorsementDetail> ctripTrainEndorsementDetails=new ArrayList<>();
        ctripTrainEndorsementDetails=convertTrainEndorsementDetail(applyInfo.getTrainEndorsementDetails());

        // 飞机信息
        if (null != ctripFlightEndorsementDetails && ctripFlightEndorsementDetails.size() > 0) {
            approvalRequest.setFlightEndorsementDetails(ctripFlightEndorsementDetails);
        }
        // 火车信息
        if (null != ctripTrainEndorsementDetails && ctripTrainEndorsementDetails.size() > 0) {
            approvalRequest.setTrainEndorsementDetails(ctripTrainEndorsementDetails);
        }
        // 住宿
        if (null != ctripHotelEndorsementDetails && ctripHotelEndorsementDetails.size() > 0) {
            approvalRequest.setHotelEndorsementDetails(ctripHotelEndorsementDetails);
        }

        if(ctripFlightEndorsementDetails.size() == 0 && ctripTrainEndorsementDetails.size() == 0 && ctripHotelEndorsementDetails.size() == 0){
            logger.info("向携程推送数据:飞机、火车、酒店数据为空、推送数据失败！");
        }else{
            approvalServiceRequest.setRequest(approvalRequest);
        }
        return  approvalServiceRequest;
    }

    private static ArrayList<FlightEndorsementDetail> convertFlightEndorsementDetail(List<ApplyInfo.FlightEndorsementDetail> flightEndorsementDetails)
    {
        ArrayList<FlightEndorsementDetail> ctripFlightEndorsementDetails = new ArrayList<>();
        if(flightEndorsementDetails!=null&&flightEndorsementDetails.size()>0)
        {
            for(int i=0;i<flightEndorsementDetails.size();i++)
            {
                ApplyInfo.FlightEndorsementDetail curFlightEndorsementDetail=flightEndorsementDetails.get(i);
                FlightEndorsementDetail flightEndorsementDetail=new FlightEndorsementDetail();
                if(curFlightEndorsementDetail.getFlightWay().equals("RoundTrip"))
                {
                    flightEndorsementDetail.setFlightWay(FlightWayType.RoundTrip);
                }
                else
                {
                    flightEndorsementDetail.setFlightWay(FlightWayType.SingleTrip);
                }
                flightEndorsementDetail.setDepartDateBegin(curFlightEndorsementDetail.getDepartDateBegin());
                flightEndorsementDetail.setDepartDateEnd(curFlightEndorsementDetail.getDepartDateEnd());
                flightEndorsementDetail.setReturnDateBegin(curFlightEndorsementDetail.getDepartDateBegin());
                ArrayList<PassengerDetail> passengerList = new ArrayList<>();
                ArrayList<Passenger> passengerAPList= curFlightEndorsementDetail.getPassengerList();
                if(passengerAPList!=null&&passengerAPList.size()>0)
                {
                    for(int j=0;j<passengerAPList.size();j++) {
                        PassengerDetail passengerDetail = new PassengerDetail();
                        passengerDetail.setName(passengerAPList.get(j).getName());
                        passengerList.add(passengerDetail);
                    }
                }
                flightEndorsementDetail.setProductType(ProductType.DomesticFlight);
                flightEndorsementDetail.setFromCities(curFlightEndorsementDetail.getFromCities());
                flightEndorsementDetail.setToCities(curFlightEndorsementDetail.getToCities());
                ctripFlightEndorsementDetails.add(flightEndorsementDetail);
            }
        }

        return ctripFlightEndorsementDetails;
    }

    private static ArrayList<HotelEndorsementDetail> convertHotelEndorsementDetail(List<ApplyInfo.HotelEndorsementDetail> hotelEndorsementDetails)
    {
        ArrayList<HotelEndorsementDetail> ctripHotelEndorsementDetails=new ArrayList<>();
        if(hotelEndorsementDetails!=null&&hotelEndorsementDetails.size()>0)
        {
            for(int i=0;i<hotelEndorsementDetails.size();i++)
            {
                ApplyInfo.HotelEndorsementDetail curHotelEndorsementDetail=hotelEndorsementDetails.get(i);
                HotelEndorsementDetail hotelEndorsementDetail=new HotelEndorsementDetail();
                hotelEndorsementDetail.setCheckInDateBegin(curHotelEndorsementDetail.getCheckInDateBegin());
                hotelEndorsementDetail.setCheckInDateEnd(curHotelEndorsementDetail.getCheckInDateEnd());
                ArrayList<PassengerDetail> passengerList = new ArrayList<>();
                List<Passenger> passengerAPList=curHotelEndorsementDetail.getPassengerList();
                if(passengerAPList!=null&&passengerAPList.size()>0)
                {
                    for(int j=0;j<passengerAPList.size();j++) {
                        PassengerDetail passengerDetail = new PassengerDetail();
                        passengerDetail.setName(passengerAPList.get(j).getName());
                        passengerList.add(passengerDetail);
                    }
                }
                hotelEndorsementDetail.setProductType(HotelProductType.Domestic);
                hotelEndorsementDetail.setToCities(curHotelEndorsementDetail.getToCities());
                hotelEndorsementDetail.setPassengerList(passengerList);
                ctripHotelEndorsementDetails.add(hotelEndorsementDetail);
            }
        }
        return  ctripHotelEndorsementDetails;
    }

    private static ArrayList<TrainEndorsementDetail> convertTrainEndorsementDetail(List<ApplyInfo.TrainEndorsementDetail> trainEndorsementDetails)
    {
        ArrayList<TrainEndorsementDetail> ctripTrainEndorsementDetails=new ArrayList<>();
        if(trainEndorsementDetails!=null&&trainEndorsementDetails.size()>0)
        {
            for(int i=0;i<trainEndorsementDetails.size();i++)
            {
                ApplyInfo.TrainEndorsementDetail curTrainEndorsementDetail=trainEndorsementDetails.get(i);
                TrainEndorsementDetail trainEndorsementDetail=new TrainEndorsementDetail();
                trainEndorsementDetail.setDepartDateBegin(curTrainEndorsementDetail.getDepartDateBegin());
                trainEndorsementDetail.setDepartDateEnd(curTrainEndorsementDetail.getDepartDateEnd());
                ArrayList<PassengerDetail> passengerList = new ArrayList<>();
                List<Passenger> passengerAPList=curTrainEndorsementDetail.getPassengerList();
                if(passengerAPList!=null&&passengerAPList.size()>0)
                {
                    for(int j=0;j<passengerAPList.size();j++) {
                        PassengerDetail passengerDetail = new PassengerDetail();
                        passengerDetail.setName(passengerAPList.get(j).getName());
                        passengerList.add(passengerDetail);
                    }
                }
                trainEndorsementDetail.setPassengerList(passengerList);
                trainEndorsementDetail.setFromCities(curTrainEndorsementDetail.getFromCities());
                trainEndorsementDetail.setToCities(curTrainEndorsementDetail.getToCities());
                ArrayList<Integer> seatTypeEnumList=new ArrayList<>();
                if(curTrainEndorsementDetail.getSeatType()!=null&&curTrainEndorsementDetail.getSeatType().size()>0)
                {
                    for (int j=0;j<curTrainEndorsementDetail.getSeatType().size();j++)
                    {
                        seatTypeEnumList.add(getSeatTypeEnum(curTrainEndorsementDetail.getSeatType().get(j)));
                    }
                }
                trainEndorsementDetail.setSeatType(seatTypeEnumList);
                ctripTrainEndorsementDetails.add(trainEndorsementDetail);
            }
        }
        return  ctripTrainEndorsementDetails;
    }

    public static CLYHApprovalInfo AppyInfoToCLYH(ApplyInfo applyInfo)
    {
        CLYHApprovalInfo clyhApprovalInfo = new CLYHApprovalInfo();
        //申请信息列表
        ArrayList<CLYHApplyInfo> clyhApplyInfos = new ArrayList<>();

        //申请信息
        CLYHApplyInfo clyhApplyInfo = convertApplyInfo(applyInfo);
        if (clyhApplyInfo != null ){
            clyhApplyInfos.add(clyhApplyInfo);
        }


        if(null == clyhApplyInfos || clyhApplyInfos.size() == 0){
            logger.info("向差旅壹号推送数据:飞机、火车、酒店数据为空、推送数据失败！");
        }else{
            clyhApprovalInfo.setApplyList(clyhApplyInfos);
        }
        return  clyhApprovalInfo;
    }

    public static CLYHApplyInfo convertApplyInfo(ApplyInfo applyInfo){
        CLYHApplyInfo clyhApplyInfo = new CLYHApplyInfo();
        ArrayList<CLYHApplyInfo.ControlInfo> controlInfos = new ArrayList<>();
        CLYHApplyInfo clyhInfo = new CLYHApplyInfo();
        CLYHApplyInfo.ControlInfo controlInfo = clyhInfo.new ControlInfo();
        ArrayList<CLYHApplyInfo.VoyageInfo> voyageInfos = new ArrayList<>();
        ArrayList<CLYHApplyInfo.StaffInfo> staffInfos = new ArrayList<>();
        Map<String,CLYHApplyInfo.StaffInfo> staffInfoMap = new HashMap<>();

        ArrayList<ApplyInfo.FlightEndorsementDetail> flightEndorsementDetails = applyInfo.getFlightEndorsementDetails();
        if (flightEndorsementDetails != null && flightEndorsementDetails.size() > 0){
            for (int i=0;i<flightEndorsementDetails.size();i++){
                ApplyInfo.FlightEndorsementDetail flightEndorsementDetail = flightEndorsementDetails.get(i);
                CLYHApplyInfo.VoyageInfo voyageInfo = clyhInfo.new VoyageInfo();
                voyageInfo.setBeginDate(flightEndorsementDetail.getDepartDateBegin());
                voyageInfo.setEndDate(flightEndorsementDetail.getDepartDateEnd());
                ArrayList<String> fromCities = flightEndorsementDetail.getFromCities();
                StringBuffer sb = new StringBuffer();
                for (int j=0;j<fromCities.size();j++){
                    if(j==fromCities.size()-1){//当循环到最后一个的时候 就不添加逗号,
                        sb.append(fromCities.get(j));
                    }
                    else {
                        sb.append(fromCities.get(j));
                        sb.append(",");
                    }
                }
                voyageInfo.setFromCity(sb.toString());
                ArrayList<String> toCities = flightEndorsementDetail.getToCities();
                StringBuffer sb1 = new StringBuffer();
                for (int j=0;j<toCities.size();j++){
                    if(j==toCities.size()-1){
                        sb1.append(toCities.get(j));
                    }else {
                        sb1.append(toCities.get(j));
                        sb1.append(",");
                    }
                }
                voyageInfo.setToCity(sb1.toString());
                //出行方式：0 飞机
                voyageInfo.setTravelType(0);
                voyageInfo.setTravelTypeName("飞机");
                if ("RoundTrip".equals(flightEndorsementDetail.getFlightWay())){
                    voyageInfo.setVoyageType(1);
                    voyageInfo.setVoyageTypeName("往返");
                }else {
                    voyageInfo.setVoyageType(0);
                    voyageInfo.setVoyageTypeName("单程");
                }

                //行程序号
                voyageInfo.setTravelNo(flightEndorsementDetail.getTravelNo());

                ArrayList<Passenger> passengerList = flightEndorsementDetail.getPassengerList();
                for (Passenger passenger : passengerList){
                    if (!staffInfoMap.containsKey(passenger.getEmpID())){
                        CLYHApplyInfo.StaffInfo staffInfo = clyhInfo.new StaffInfo();
                        //添加人员信息
                        staffInfo.setStaffName(passenger.getName());
                        staffInfo.setEmpID(passenger.getEmpID());
                        staffInfo.setDepartment(passenger.getDepartment());
                        staffInfo.setDepartmentNO(passenger.getDepartmentNO());
                        staffInfoMap.put(passenger.getEmpID(),staffInfo);
                    }
                }

                voyageInfos.add(voyageInfo);

            }
        }

        ArrayList<ApplyInfo.OtherEndorsementDetail> otherEndorsementDetails = applyInfo.getOtherEndorsementDetails();
        if (null != otherEndorsementDetails && otherEndorsementDetails.size() > 0){
            for (int i=0;i<otherEndorsementDetails.size();i++) {
                ApplyInfo.OtherEndorsementDetail otherEndorsementDetail = otherEndorsementDetails.get(i);
                CLYHApplyInfo.VoyageInfo voyageInfo = clyhInfo.new VoyageInfo();
                voyageInfo.setBeginDate(otherEndorsementDetail.getDepartDateBegin());
                voyageInfo.setEndDate(otherEndorsementDetail.getDepartDateEnd());
                ArrayList<String> fromCities = otherEndorsementDetail.getFromCities();
                StringBuffer sb = new StringBuffer();
                for (int j = 0; j < fromCities.size(); j++) {
                    if (j == fromCities.size() - 1){//当循环到最后一个的时候 就不添加逗号,
                        sb.append(fromCities.get(j));
                    } else {
                        sb.append(fromCities.get(j));
                        sb.append(",");
                    }
                }
                voyageInfo.setFromCity(sb.toString());
                ArrayList<String> toCities = otherEndorsementDetail.getToCities();
                StringBuffer sb1 = new StringBuffer();
                for (int j = 0; j < toCities.size(); j++) {
                    if (j == toCities.size() - 1) {
                        sb1.append(toCities.get(j));
                    } else {
                        sb1.append(toCities.get(j));
                        sb1.append(",");
                    }
                }
                voyageInfo.setToCity(sb1.toString());
                //出行方式：其他
                String travelTypeName = otherEndorsementDetail.getTravelTypeName();
                if ("火车".equals(travelTypeName)){
                    voyageInfo.setTravelType(1);
                    voyageInfo.setTravelTypeName("火车");
                } else if ("汽车".equals(travelTypeName)){
                    voyageInfo.setTravelType(2);
                    voyageInfo.setTravelTypeName("汽车");
                } else {
                    voyageInfo.setTravelType(3);
                    voyageInfo.setTravelTypeName("其他");
                }

                if ("RoundTrip".equals(otherEndorsementDetail.getVoyageType())) {
                    voyageInfo.setVoyageType(1);
                    voyageInfo.setVoyageTypeName("往返");
                } else {
                    voyageInfo.setVoyageType(0);
                    voyageInfo.setVoyageTypeName("单程");
                }

                //行程序号
                voyageInfo.setTravelNo(otherEndorsementDetail.getTravelNo());

                ArrayList<Passenger> passengerList = otherEndorsementDetail.getPassengerList();
                for (Passenger passenger : passengerList) {
                    if (!staffInfoMap.containsKey(passenger.getEmpID())) {
                        CLYHApplyInfo.StaffInfo staffInfo = clyhInfo.new StaffInfo();
                        //添加人员信息
                        staffInfo.setStaffName(passenger.getName());
                        staffInfo.setEmpID(passenger.getEmpID());
                        staffInfo.setDepartment(passenger.getDepartment());
                        staffInfo.setDepartmentNO(passenger.getDepartmentNO());
                        staffInfoMap.put(passenger.getEmpID(),staffInfo);
                    }
                }
                voyageInfos.add(voyageInfo);
            }
        }

        for (Map.Entry<String, CLYHApplyInfo.StaffInfo> staffInfoEntry : staffInfoMap.entrySet()){
            staffInfos.add(staffInfoEntry.getValue());
        }

        if (controlInfos != null && controlInfos.size() > 0){
            clyhApplyInfo.setControlInfo(controlInfos);
        }
        if (staffInfos != null && staffInfos.size() > 0){
            clyhApplyInfo.setStaffList(staffInfos);
        }
        if (voyageInfos != null && voyageInfos.size() > 0){
            clyhApplyInfo.setVoyageList(voyageInfos);
        }

        return  clyhApplyInfo;
    }


    private static int getSeatTypeEnum(String seatType)
    {
        SeatTypeEnum seatTypeEnum=null;
        switch (seatType)
        {
            case "YZ":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "RZ":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "TDZ":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "EDZ":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "SWZ":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "YW":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "RW":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "DW":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "DJRW":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "YDRZ":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "EDRZ":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "YRRB":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
            case "GJDW":
                seatTypeEnum=SeatTypeEnum.YZ;
                break;
        }
        return  seatTypeEnum.getValue();
    }
}
